Key Remapping in Mapped Types
Key Remapping in Mapped Types
key remapping via as
こう書けるようになった
code:ts
type MappedTypeWithNewKeys<T> = {
// ^^^^^^^^^^^^^
// This is the new syntax!
}
mapする際のkey側を変更できるようになった
code:ts
type Getters<T> = {
[K in keyof T as get${Capitalize<string & K>}]: () => TK };
interface Person {
name: string;
age: number;
location: string;
}
type LazyPerson = Getters<Person>;
// ^ = type LazyPerson = {
// getName: () => string;
// getAge: () => number;
// getLocation: () => string;
// }
さらにremapの際にneverを渡すことで、除外できる
code:ts
// Remove the 'kind' property
type RemoveKindField<T> = {
};
interface Circle {
kind: "circle";
radius: number;
}
type KindlessCircle = RemoveKindField<Circle>;
// ^ = type KindlessCircle = {
// radius: number;
// }
バグ
code:ts
type Mapped<T> = {
}
type MappedAs<T> = {
}
type test0 = Mapped<tuple> // tuple shape is preserved
type test1 = MappedAs<tuple> // tuple shape is object now
TSで型パズル気合い入れるとすぐ使いたくなる割には、バグが昨年から放置されてるな